package com.ybkj.daijia.server.controller.vehicleInfo.app;

import com.ybkj.daijia.api.common.responseBody.ResponseCode;
import com.ybkj.daijia.api.common.responseBody.ResponseResult;
import com.ybkj.daijia.server.service.vehicleInfo.VehicleService;
import com.ybkj.daijia.server.service.vehicleInfo.VehicleUsedStatusService;
import com.ybkj.daijia.server.vehicleInfo.Vehicle;
import com.ybkj.daijia.server.vehicleInfo.VehicleUsedStatus;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/***
 * @author: huguigang
 * @date: 2019-12-12
 * @description: app车辆数据接口
 */
@Controller
@RequestMapping(value = "app/vehicle")
public class VehicleAppController {

    @Autowired
    private VehicleService vehicleService;

    @Autowired
    private VehicleUsedStatusService vehicleUsedStatusService;

    /**
     * 根据公司id和车牌号模糊查询未绑定车辆
     * @param companyId 公司id
     * @param vehicleNumber 车牌号（不一定是完整的车牌号，模糊查询）
     * @return
     */
    @RequestMapping(value = "getUnbindVehicles")
    @ResponseBody
    public String getUnbindVehicles(@RequestParam("companyId") Long companyId, @RequestParam(value = "vehicleNumber", required = false) String vehicleNumber){
        ResponseResult rr = new ResponseResult(ResponseCode.SUCCESS);

        //如果上传了车牌号参数
        if(StringUtils.isNotBlank(vehicleNumber)){
            List<Vehicle> vehicles = vehicleService.getCarByCarNumber(vehicleNumber);
            //车辆不存在
            if(null == vehicles || vehicles.size() < 1){
                return new ResponseResult(ResponseCode.CAR_NONEXITENT).getResultJsonString();
            }
            //查询车辆是否被绑定
            VehicleUsedStatus vus = vehicleUsedStatusService.getBoundVehicleByVehicleNumber(vehicleNumber);
            //车辆已被绑定
            if(null != vus){
                return new ResponseResult(ResponseCode.VEHICLE_IS_BOUND).getResultJsonString();
            }
        }
        //查询该公司未绑定的车辆集合
        List<Vehicle> vehicleList = vehicleService.getVehicleList(companyId, vehicleNumber);
        rr.setData(vehicleList);
        return rr.getResultJsonString();
    }

    /**
     * 驾驶员绑定车辆
     * @param driverId 驾驶员id
     * @param vehicleId 车辆id
     * @return
     */
    @RequestMapping(value = "bindingVehicle", method = RequestMethod.POST)
    @ResponseBody
    public String bindingDriverAndVehicle(@RequestParam(value = "driverId") String driverId, @RequestParam(value = "vehicleId") String vehicleId){

        return new ResponseResult(ResponseCode.SUCCESS).getResultJsonString();
    }
}
